package org.brisling.sysman.queryService;

import java.util.HashMap;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.brisling.base.domain.ListDomain;
import org.brisling.base.service.AbstractBaseQueryService;
import org.brisling.common.ConstSysParam;
import org.brisling.common.SysConfig;
import org.brisling.sysman.domain.SysShortMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
@RequestMapping("/sms/query")
public class QueryShortMessageService extends AbstractBaseQueryService<SysShortMessage>{
	
	
	public QueryShortMessageService(){
		this.moduleName = ConstSysParam.MODULE_SHORTMESSAGE;
		this.subSystem = ConstSysParam.SUBSYSTEM_SYSMAN;
		this.tableName = ConstSysParam.MODULE_SHORTMESSAGE;
		this.clazz = SysShortMessage.class;
		
		typeMap = this.getTypeMap(clazz);		
		
		orderMap.put("createTime","desc");	
		
	}
	
	@PostConstruct
	public void initialize(){
		HashMap<String,String> maps= SysConfig.getMaps();
		if(maps==null || maps.isEmpty()){
			HashMap<String,String> paramMap = this.initMaps();
			if(paramMap!=null)
				SysConfig.setMaps(paramMap);
		}
	}
	
	@PersistenceContext(unitName=ConstSysParam.SUBSYSTEM_SYSMAN)
	@Autowired
	protected void setEntityManager(EntityManager entityManager){
		this.entityManager = entityManager;		
	}
	
	@RequestMapping("/mainPage.action")
	public ModelAndView getMainPage(){
		return this.getBizMainPage();
	}
	
	/* (non-Javadoc)
	 * @see org.brisling.service.sysman.DepartmentServiceInf#getEditPage()
	 */
	@RequestMapping("/editPage.action")
	public ModelAndView getEditPage(){
		return this.getBizEditPage();
	}
	
	
	
	@RequestMapping("/getListDomain.action")
	public ListDomain<SysShortMessage> getListDomain(
			@RequestBody(required=false) String body){		
		
		return super.getListDomainByConditionAndOrder(body, typeMap, orderMap,true);
	}
	
	@RequestMapping("/getList.action")
	public List<SysShortMessage> getList(){		
		
		
		String sql = String.format(this.T_SQL, this.tableName);
		
		return super.getAllList(sql);	
		
	}
	
	/**
	 * 根据用户id查找用户信息
	 * @param id	用户id
	 * @return 用户信息记录 
	 */
	@RequestMapping("/getOne.action")
	public SysShortMessage getOne(@RequestParam(value="id",required=false) Long id){
		return super.getOne(id);
	}
	
	/**
	 * 根据用户唯一编码查找用户信息
	 * @param identify	用户唯一编码
	 * @return 用户信息记录
	 */
	@RequestMapping("/getOneByIdentify.action")
	public SysShortMessage getOneByIdentify(@RequestParam(value="identify",required=false) String identify){	
		return super.getOneByIdentify(identify);
	}
	
	@RequestMapping("/reloadCache.action")
	public String reloadCache(){		
		SysConfig.setMaps(this.initMaps());
		return "200";
	}
	
	/**
	 * 查询系统配置参数集合
	 * @return
	 */
	private HashMap<String,String> initMaps(){
		HashMap<String,String> maps = null;
		List<SysShortMessage> lists = this.getList();
		if(lists!=null &&(!lists.isEmpty())){
			maps = new HashMap<String,String>();
			for(SysShortMessage p:lists){
				maps.put(p.getBizno(), p.getValue());
			}
		}
		
		return maps;
	}
	
}
